package user

import (
	"time"

	"gitee.com/xiaoyutab/gatway/config"
	"gitee.com/xiaoyutab/gatway/model"
	"gitee.com/xiaoyutab/xgotool/xerror"
)

// 记录用户操作日志
//
//	uid	用户ID
//	typ	操作类型 0-登录 1-注册 2-重置密码 3-密码输入错误 4-密码输入错误导致锁定
//	ip	用户操作客户端IP
func Log(uid uint, typ uint8, ip string) error {
	if uid == 0 {
		return xerror.New("用户ID为空")
	}
	inf := model.UserLoginlog{
		Uid:       uid,
		Type:      typ,
		CreatedAt: time.Now().Format(time.DateTime),
		CreatedIp: ip,
	}
	db := config.DB.Table(inf.TableName()).Create(&inf)
	if db.Error != nil {
		return xerror.To(db.Error)
	}
	if db.RowsAffected <= 0 {
		return xerror.New("用户操作日志记录失败")
	}
	return nil
}
